package cn.car.biz.impl;

import cn.car.biz.SysPermissionBiz;
import cn.car.domains.bo.QueryPermissionBo;
import cn.car.domains.entity.SysPermissionEntity;
import cn.car.domains.enums.ActiveStatusEnum;
import cn.car.domains.vo.BasePage;
import cn.car.domains.vo.sysPermission.CreatePermissionReq;
import cn.car.domains.vo.sysPermission.PermissionListReq;
import cn.car.domains.vo.sysPermission.SysPermissionRes;
import cn.car.domains.vo.sysPermission.UpdatePermissionReq;
import cn.car.exception.CarAccessControlException;
import cn.car.service.SysPermissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysPermissionBizImpl implements SysPermissionBiz {


    @Resource
    private SysPermissionService sysPermissionService;

    @Override
    public void createPermission(CreatePermissionReq req) {
        SysPermissionEntity sysPermissionEntity = new SysPermissionEntity();
        BeanUtils.copyProperties(req, sysPermissionEntity);

        sysPermissionEntity.setStatus(ActiveStatusEnum.ACTIVE.name());
        sysPermissionService.addPermission(sysPermissionEntity);
    }

    @Override
    public SysPermissionRes getPermissionById(Long id) {
        SysPermissionEntity sysPermissionEntity = sysPermissionService.getPermissionById(id);
        if (sysPermissionEntity == null) {
            return null;
        }
        SysPermissionRes res = new SysPermissionRes();
        BeanUtils.copyProperties(sysPermissionEntity, res);
        return res;
    }

    @Override
    public SysPermissionRes getPermissionByCode(String permissionCode) {
        SysPermissionEntity sysPermissionEntity = sysPermissionService.getPermissionByCode(permissionCode);
        if (sysPermissionEntity == null) {
            return null;
        }
        SysPermissionRes res = new SysPermissionRes();
        BeanUtils.copyProperties(sysPermissionEntity, res);
        return res;
    }

    @Override
    public void updatePermission(UpdatePermissionReq req) {
        SysPermissionEntity sysPermissionEntity = sysPermissionService.getPermissionById(req.getId());
        if (sysPermissionEntity == null) {
            throw CarAccessControlException.DATE_NOT_EXIST.newInstance("权限不存在");
        }
        SysPermissionEntity updateEntity = new SysPermissionEntity();
        BeanUtils.copyProperties(req, updateEntity);
        sysPermissionService.updatePermission(updateEntity);
    }

    @Override
    public void deletePermission(Long id) {
        SysPermissionEntity sysPermissionEntity = sysPermissionService.getPermissionById(id);
        if (sysPermissionEntity == null) {
            throw CarAccessControlException.DATE_NOT_EXIST.newInstance("权限不存在");
        }
        sysPermissionService.deletePermission(id);
    }

    @Override
    public BasePage<SysPermissionRes> list(PermissionListReq req) {
        Page<Object> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        QueryPermissionBo query = new QueryPermissionBo();
        query.setPermissionCode(req.getPermissionCode());
        query.setPermissionType(req.getPermissionType());
        query.setPermissionName(req.getPermissionName());
        List<SysPermissionEntity> sysPermissionEntityList = sysPermissionService.list(query);
        List<SysPermissionRes> resList = sysPermissionEntityList.stream().map(entity -> {
            SysPermissionRes res = new SysPermissionRes();
            BeanUtils.copyProperties(entity, res);
            return res;
        }).toList();
        return BasePage.generateResult(page.getPages(), page.getTotal(), resList);
    }
}
